<?php

class RegistrationController extends BaseController
{
	public function actions()
	{
		return array(
			'captcha'=>array(
                'class'=>'CCaptchaAction',
                'width'=>'120',
                'height'=>'50',
                'maxLength' => '5',
                'minLength' =>'3',
                'backColor'=>0xFFFFFF,
            )
		);
	}

	public function actionIndex()
	{
		$this->render('registration');
	}
	
	//регистрация клиента
	public function actionClient()
	{
		$user = new User(User::SCENARIO_SIGNUP);
		$client = new Client();

		if(Yii::app()->request->isPostRequest)
		{
			$user->attributes = $_POST['User'];
			$user->type = User::TYPE_CLIENT;
			$client->attributes = $_POST['Client'];
			if($user->validate() && $client->validate())
			{
				$user->pass = PasswordHelper::hashPassword($user->pass);
				if($user->save(false))
				{
			 		$mailRes = Mailer::send(array(
			 				'from'		=> Yii::app()->params['adminEmail'],
			 				'to'		=> $user->email,
			 				'layout'	=> 'common',
			 				'view'		=> 'registration',
			 				'type'		=> 'text/html',
			 		
			 				'site'		=> Yii::app()->getRequest()->getHostInfo(),
			 				'id'		=> $user->id,
			 				'hash'		=> $this->getConfirmHash($user)
			 		));
			 			
			 			
			 		if ($mailRes)
			 		{
			 			$client->id_user = Yii::app()->db->getLastInsertID();
			 			$client->save();
			 		
			 			$this->render("registration_ok");
			 			Yii::app()->end();
			 		}
			 		else
			 		{
			 			/* Действия если не удалось отправить письмо на почту */
			 			/*
			 			 $user->delete();
			 				
			 			$this->render("/shared/message_error", array(
			 					'data' => array('Не удалось отправить сообщение на почту')
			 			));*/
			 		}
			 	}
			}
		}
		$this->render('registration_client', array('user' => $user,'client' => $client));
	}
	
	/* регистрация поставщика @[fan-cpp] */
	public function actionShop()
	{
		$user = new User(User::SCENARIO_SIGNUP);
		$shop = new Shop(User::SCENARIO_SIGNUP);
	
		if(Yii::app()->request->isPostRequest)
		{
			$user->attributes = $_POST['User'];
			$user->type = User::TYPE_SHOP_ADMIN;
			$shop->attributes = $_POST['Shop'];
			
			if($user->validate() && $shop->validate())
			{
				$user->pass = PasswordHelper::hashPassword($user->pass);
				if($user->save(false))
				{
					$mailRes = Mailer::send(array(
						'from'		=> Yii::app()->params['adminEmail'],
						'to'		=> $user->email,
						'layout'	=> 'common',
						'view'		=> 'registration',
						'type'		=> 'text/html',
				
						'site'		=> Yii::app()->getRequest()->getHostInfo(),
						'id'		=> $user->id,
						'hash'		=> $this->getConfirmHash($user)
					));
					
					
					if ($mailRes)
					{
						$shop->id_user = Yii::app()->db->getLastInsertID();
						$shop->save();
						
						$this->render("registration_ok");
						Yii::app()->end();
					}
					else
					{
						/* Действия если не удалось отправить письмо на почту */
						/*
						$user->delete();
							
						$this->render("/shared/message_error", array(
								'data' => array('Не удалось отправить сообщение на почту')
						));*/
					}
				}
			}
		}
		$this->render('registration_shop', array('user' => $user, 'shop' => $shop));
	}
	
	/* Подтверждение регистрации @[fan-cpp] */
	public function actionConfirm($id, $hash)
	{
		$user = User::model()->findByPk($id);
		
		if ($hash == $this->getConfirmHash($user) && $user->state != 1)
		{
			$user->state = 1;
			$user->save();
			$user->loginByHash($user);
			
			$this->render('confirm_account_ok');
		}
		else
		{
			throw new CHttpException(404, 'Указанная страница не найдена');
		}
	}
	
	/* получить hash-строку пользователя @[fan-cpp] */
	private function getConfirmHash(User $user)
	{
		return PasswordHelper::hashPassword($user->pass . $user->email . $user->pass) . PasswordHelper::hashPassword($user->email . $user->pass . $user->email);;
	}
}